package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.LoginVo;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/29 15:20
 * @Version 1.0
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(LoginVo loginVo) {
        //  select * from user_info where (login_name = ? or email = ? or phone_num = ?) and passwd = ?;
        //  userInfoMapper.login(loginVo); 使用mapper
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //        wrapper.eq(UserInfo::getLoginName,loginVo.getLoginName())
        //                        .or().eq(UserInfo::getEmail,loginVo.getLoginName())
        //                .or().eq(UserInfo::getPhoneNum,loginVo.getLoginName());
        //  让以下条件变一个整体，关联是 or 关系。
        userInfoLambdaQueryWrapper.or(wrapper->{
            wrapper.eq(UserInfo::getLoginName,loginVo.getPhone())
                    .or().eq(UserInfo::getEmail,loginVo.getPhone())
                    .or().eq(UserInfo::getPhoneNum,loginVo.getPhone());
        });
        //  96e79218965eb72c92a549dd5a330112
        userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd, MD5.encrypt(loginVo.getPassword()));
        UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        return userInfo;
    }
}
